home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / POV-Ray 3.0.2 / src / SOURCE / LIBPNG / PNGTRANS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-06  |  5.3 KB  |  229 lines  |  [TEXT/CWIE]

  1.  
  2. /* pngtrans.c - transforms the data in a row
  3.    routines used by both readers and writers
  4.  
  5.    libpng 1.0 beta 4 - version 0.90
  6.    For conditions of distribution and use, see copyright notice in png.h
  7.    Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  8.    January 10, 1997
  9.    */
  10.  
  11. #define PNG_INTERNAL
  12. #include "png.h"
  13.  
  14. #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
  15. /* turn on bgr to rgb mapping */
  16. void
  17. png_set_bgr(png_structp png_ptr)
  18. {
  19.    png_ptr->transformations |= PNG_BGR;
  20. }
  21. #endif
  22.  
  23. #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
  24. /* turn on 16 bit byte swapping */
  25. void
  26. png_set_swap(png_structp png_ptr)
  27. {
  28.    if (png_ptr->bit_depth == 16)
  29.       png_ptr->transformations |= PNG_SWAP_BYTES;
  30. }
  31. #endif
  32.  
  33. #if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
  34. /* turn on pixel packing */
  35. void
  36. png_set_packing(png_structp png_ptr)
  37. {
  38.    if (png_ptr->bit_depth < 8)
  39.    {
  40.       png_ptr->transformations |= PNG_PACK;
  41.       png_ptr->usr_bit_depth = 8;
  42.    }
  43. }
  44. #endif
  45.  
  46. #if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
  47. void
  48. png_set_shift(png_structp png_ptr, png_color_8p true_bits)
  49. {
  50.    png_ptr->transformations |= PNG_SHIFT;
  51.    png_ptr->shift = *true_bits;
  52. }
  53. #endif
  54.  
  55. #if defined(PNG_READ_INTERLACING_SUPPORTED) || defined(PNG_WRITE_INTERLACING_SUPPORTED)
  56. int
  57. png_set_interlace_handling(png_structp png_ptr)
  58. {
  59.    if (png_ptr->interlaced)
  60.    {
  61.       png_ptr->transformations |= PNG_INTERLACE;
  62.       return 7;
  63.    }
  64.  
  65.    return 1;
  66. }
  67. #endif
  68.  
  69. #if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
  70. void
  71. png_set_filler(png_structp png_ptr, png_byte filler, int filler_loc)
  72. {
  73.    png_ptr->transformations |= PNG_FILLER;
  74.    png_ptr->filler = filler;
  75.    if (filler_loc == PNG_FILLER_AFTER)
  76.       png_ptr->flags |= PNG_FLAG_FILLER_AFTER;
  77.    else
  78.       png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER;
  79.  
  80.    if (png_ptr->color_type == PNG_COLOR_TYPE_RGB &&
  81.       png_ptr->bit_depth == 8)
  82.       png_ptr->usr_channels = 4;
  83. }
  84.  
  85. /* Old functions kept around for compatability purposes.  They will be
  86.  * removed at some time in the future, so don't use them.  You should
  87.  * use png_set_filler() above instead.  We set filler bytes to 0xff in
  88.  * case they are mistakenly used as PNG alpha (0xff is fully opaque). */
  89. void
  90. png_set_rgbx(png_structp png_ptr)
  91. {
  92.    png_set_filler(png_ptr, (png_byte)0xff, PNG_FILLER_AFTER);
  93. }
  94.  
  95. void
  96. png_set_xrgb(png_structp png_ptr)
  97. {
  98.    png_set_filler(png_ptr, (png_byte)0xff, PNG_FILLER_BEFORE);
  99. }
  100. #endif
  101.  
  102. #if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
  103. void
  104. png_set_invert_mono(png_structp png_ptr)
  105. {
  106.    png_ptr->transformations |= PNG_INVERT_MONO;
  107. }
  108.  
  109. /* invert monocrome grayscale data */
  110. void
  111. png_do_invert(png_row_infop row_info, png_bytep row)
  112. {
  113.    if (row && row_info && row_info->bit_depth == 1 &&
  114.       row_info->color_type == PNG_COLOR_TYPE_GRAY)
  115.    {
  116.       png_bytep rp;
  117.       png_uint_32 i;
  118.  
  119.       for (i = 0, rp = row;
  120.          i < row_info->rowbytes;
  121.          i++, rp++)
  122.       {
  123.          *rp = (png_byte)(~(*rp));
  124.       }
  125.    }
  126. }
  127. #endif
  128.  
  129. #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
  130. /* swaps byte order on 16 bit depth images */
  131. void
  132. png_do_swap(png_row_infop row_info, png_bytep row)
  133. {
  134.    if (row && row_info && row_info->bit_depth == 16)
  135.    {
  136.       png_bytep rp;
  137.       png_byte t;
  138.       png_uint_32 i;
  139.  
  140.       for (i = 0, rp = row;
  141.          i < row_info->width * row_info->channels;
  142.          i++, rp += 2)
  143.       {
  144.          t = *rp;
  145.          *rp = *(rp + 1);
  146.          *(rp + 1) = t;
  147.       }
  148.    }
  149. }
  150. #endif
  151.  
  152. #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
  153. /* swaps red and blue */
  154. void
  155. png_do_bgr(png_row_infop row_info, png_bytep row)
  156. {
  157.    if (row && row_info && (row_info->color_type & 2))
  158.    {
  159.       if (row_info->color_type == 2 && row_info->bit_depth == 8)
  160.       {
  161.          png_bytep rp;
  162.          png_byte t;
  163.          png_uint_32 i;
  164.  
  165.          for (i = 0, rp = row;
  166.             i < row_info->width;
  167.             i++, rp += 3)
  168.          {
  169.             t = *rp;
  170.             *rp = *(rp + 2);
  171.             *(rp + 2) = t;
  172.          }
  173.       }
  174.       else if (row_info->color_type == 6 && row_info->bit_depth == 8)
  175.       {
  176.          png_bytep rp;
  177.          png_byte t;
  178.          png_uint_32 i;
  179.  
  180.          for (i = 0, rp = row;
  181.             i < row_info->width;
  182.             i++, rp += 4)
  183.          {
  184.             t = *rp;
  185.             *rp = *(rp + 2);
  186.             *(rp + 2) = t;
  187.          }
  188.       }
  189.       else if (row_info->color_type == 2 && row_info->bit_depth == 16)
  190.       {
  191.          png_bytep rp;
  192.          png_byte t[2];
  193.          png_uint_32 i;
  194.  
  195.          for (i = 0, rp = row;
  196.             i < row_info->width;
  197.             i++, rp += 6)
  198.          {
  199.             t[0] = *rp;
  200.             t[1] = *(rp + 1);
  201.             *rp = *(rp + 4);
  202.             *(rp + 1) = *(rp + 5);
  203.             *(rp + 4) = t[0];
  204.             *(rp + 5) = t[1];
  205.          }
  206.       }
  207.       else if (row_info->color_type == 6 && row_info->bit_depth == 16)
  208.       {
  209.          png_bytep rp;
  210.          png_byte t[2];
  211.          png_uint_32 i;
  212.  
  213.          for (i = 0, rp = row;
  214.             i < row_info->width;
  215.             i++, rp += 8)
  216.          {
  217.             t[0] = *rp;
  218.             t[1] = *(rp + 1);
  219.             *rp = *(rp + 4);
  220.             *(rp + 1) = *(rp + 5);
  221.             *(rp + 4) = t[0];
  222.             *(rp + 5) = t[1];
  223.          }
  224.       }
  225.    }
  226. }
  227. #endif
  228.  
  229.